package it.netsphere.nswing.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;



public class PropertyMethodMap
{
	
	private Map<Class<?>,Map<String,Method>> readMap = new HashMap<Class<?>, Map<String,Method>>();
	
	private Map<String,Method> getClassReadMethods( Class<?> cl )
		throws IntrospectionException
	{
		Map<String,Method> m = readMap.get( cl );
		
		if(m==null)
		{
			m = new HashMap<String, Method>();
	        BeanInfo info = Introspector.getBeanInfo( cl );
	        for( PropertyDescriptor pd:  info.getPropertyDescriptors() )
	        {
	        	m.put( pd.getName() , pd.getReadMethod() );
	        }
	        
	        readMap.put( cl , m);
		}
		
		return m;
	}
	
	public Method getReadMethod(Class<?> cl , String propertyName )
		throws IntrospectionException
	{
		Map<String,Method> methods = getClassReadMethods(cl);
		Method meth  = methods.get( propertyName );
		
		return meth;
	}
}
